{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "pandas是基于Numpy构建的。  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Series是什么\n",
    "Series：一维数组，与Numpy中的一维数组类似。  \n",
    "二者与Python基本的数据结构List也很相近，其区别是：  \n",
    "List中的元素可以是不同的数据，而Array和Series中则只能存储相同类型的数据，这样可以更有效地使用内存，提高运算效率。  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 创建Series\n",
    "通过给pd.Series()方法传入不同的对象来创建一个Series。\n",
    "## 传入列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    一班\n",
      "1    一班\n",
      "2    三班\n",
      "3    二班\n",
      "4    一班\n",
      "dtype: object\n",
      "a    一班\n",
      "b    一班\n",
      "c    三班\n",
      "d    二班\n",
      "e    一班\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "class_list = [\"一班\", \"一班\", \"三班\", \"二班\", \"一班\"]\n",
    "index = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
    "\n",
    "# 默认索引\n",
    "S1 = pd.Series(class_list)\n",
    "# 指定索引\n",
    "S2 = pd.Series(class_list, index)\n",
    "\n",
    "print(S1)\n",
    "print(S2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 传入字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "d    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict_a = {\"a\": 1, \"b\": 2, \"c\": 3, \"d\": 4}\n",
    "S22 = pd.Series(dict_a)\n",
    "S22"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 传入元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    a\n",
       "1    b\n",
       "2    c\n",
       "3    d\n",
       "dtype: object"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple_a = ('a','b','c','d')\n",
    "S4 = pd.Series(tuple_a)\n",
    "S4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取Series的索引\n",
    "使用index()方法获取Series的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RangeIndex(start=0, stop=5, step=1)\n",
      "Index(['a', 'b', 'c', 'd', 'e'], dtype='object')\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "score_list = [\"一班\", \"一班\", \"三班\", \"二班\", \"一班\"]\n",
    "index = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
    "\n",
    "S31 = pd.Series(score_list)\n",
    "S32 = pd.Series(score_list, index)\n",
    "\n",
    "print(S31.index)\n",
    "print(S32.index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取Series的值\n",
    "使用values方法获取Series的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['一班' '一班' '三班' '二班' '一班']\n",
      "['一班' '一班' '三班' '二班' '一班']\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "score_list = [\"一班\", \"一班\", \"三班\", \"二班\", \"一班\"]\n",
    "index = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
    "\n",
    "# 创建Series\n",
    "S41 = pd.Series(score_list)\n",
    "S42 = pd.Series(score_list, index)\n",
    "\n",
    "# 获取Series的值\n",
    "print(S41.values)\n",
    "print(S42.values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method IndexOpsMixin.item of a    一班\n",
       "b    一班\n",
       "c    三班\n",
       "d    二班\n",
       "e    一班\n",
       "dtype: object>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取Series的键值\n",
    "S42.item"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('O')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S42.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
